package com.mm.learn.shopping.cart.vo;/**
 * @author wdx
 * @create 2021-07-26 16:58
 */


import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author wdx
 * @date 2021年07月26日 16:58
 */
@Data
public class Cart {

    List<CartItem> items;
    /**
     * 商品数量
     */
    private int countNum;
    /**
     * 商品类型数量
     */
    private int countType;
    /**
     * 商品总价
     */
    private BigDecimal totalAmount;
    /**
     * 减免价格
     */
    private BigDecimal reduce = new BigDecimal(0);

    public List<CartItem> getItems() {
        return items;
    }

    public void setItems(List<CartItem> items) {
        this.items = items;
    }

    public int getCountNum() {
        int count = 0;
        if (items != null && !items.isEmpty()){
            for (CartItem item : items) {
                count += item.getCount();
            }
        }
        return count;
    }


    public Integer getCountType() {
        int count = 0;
        if (items != null && !items.isEmpty()){
            for (CartItem item : items) {
                count += 1;
            }
        }
        return count;
    }

    public BigDecimal getTotalAmount() {
        BigDecimal amount = new BigDecimal(0);
        if (items != null && !items.isEmpty()){
            for (CartItem item : items) {
                if (item.getCheck()){
                    amount = amount.add(item.getTotalPrice());
                }
            }
        }
        //减去优惠总价
        BigDecimal subtract = amount.subtract(getReduce());
        return subtract;
    }

    @Override
    public String toString() {
        return "Cart{" +
                "items=" + items +
                ", countNum=" + countNum +
                ", countType=" + countType +
                ", totalAmount=" + totalAmount +
                ", reduce=" + reduce +
                '}';
    }

    public BigDecimal getReduce() {
        return reduce;
    }

    public void setReduce(BigDecimal reduce) {
        this.reduce = reduce;
    }
}
